package xin.alum.aim;

import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Component;
import xin.alum.aim.groups.SessionGroups;
import xin.alum.aim.groups.Sessions;
import xin.alum.aim.model.Pusher;
import xin.alum.aim.util.ApplicationContextUtil;

@Component
public class ClusterMessage implements xin.alum.aim.groups.ClusterMessage {
    private final Sessions sessions;

    private final SessionGroups groups;

    public ClusterMessage(Sessions sessions, SessionGroups groups) {
        this.sessions = sessions;
        this.groups = groups;
    }

    @Override
    public void onMessage(byte[] b) {
        Jackson2JsonRedisSerializer json = ApplicationContextUtil.getBean(Jackson2JsonRedisSerializer.class);
        Pusher p = (Pusher) json.deserialize(b);
        groups.get(p.getRecipient()).sends(p.getData());
    }
}
